package longrise.phone.com.bjjt_jyb.compensate;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.CheckCard;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.StringUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;
import longrise.phone.com.bjjt_jyb.widget.SetTextModule;


/**
 * 事故上传的信息填写界面，Created by Administrator on 2016/5/12.
 */
public class AccidentLoadingView extends LFView implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, ILFMsgListener {
    private String appCaseNo;//案件编号
    private Context context = null;
    private View view = null;
    private Button bt_accidentloading;
    private BaseApplication mApplication;
    private Dialog processDialog;
    private String jfCarType;
    private String yfCarType;
    private LinearLayout hn_btn_back;
    private String accidentType;
    private String discription;
    private boolean checked1;
    private boolean checked2;
    private String imageaddress;
    private SetTextModule stv_MotorVehicle_chooseCartypePartyJ;
    private RadioGroup radioGroupMotorVehiclePartyJ;
    private SetTextModule stv_MotorVehicle_NamePartyJ;
    private SetTextModule stv_MotorVehicle_carNoPartyJ;
    private TextView motorVehicle_provinceFirstViewPartyJ;
    private SetTextModule stv_MotorVehicle_carFrameNoPartyJ;
    private SetTextModule stv_MotorVehicle_PhonePartyJ;
    private SetTextModule stv_MotorVehicle_IDPartyJ;
    private SetTextModule stv_MotorVehicle_InsurancePartyJ;
    private TextView gainTextViewPartyJ;
    private SetTextModule stv_MotorVehicle_chooseCartypePartyY;
    private RadioGroup radioGroupMotorVehiclePartyY;
    private SetTextModule stv_MotorVehicle_NamePartyY;
    private SetTextModule stv_MotorVehicle_carNoPartyY;
    private TextView motorVehicle_provinceFirstViewPartyY;
    private SetTextModule stv_MotorVehicle_carFrameNoPartyY;
    private SetTextModule stv_MotorVehicle_PhonePartyY;
    private SetTextModule stv_MotorVehicle_IDPartyY;
    private SetTextModule stv_MotorVehicle_InsurancePartyY;
    private TextView gainTextViewPartyY;
    private String kckpUserType;
    private int carNumber = 1;
    private EditText et_accidentSingle;
    private TextView tv_accidentSinglePlace;
    private String accidentPlace;//单车事故地点
    private String nameJ;//甲方姓名
    private String firstCarNumJ;//甲方车牌首字母
    private String secendCarNumJ;//甲方车牌后尾
    private String carNumJ;//甲方车牌号
    private String carVINJ;//甲方车架号
    private String phoneJ;//甲方手机
    private String idCardJ;//甲方驾驶证
    private String insuranceJ;
    private String nameY;
    private String firstCarNumY;
    private String secendCarNumY;
    private String carNumY;
    private String carVINY;
    private String phoneY;
    private String idCardY;
    private String insuranceY;
    private String casehaptime;
    private TextView tv_accidentSingleTime;
    private LinearLayout ll_accidentTime;

    public AccidentLoadingView(Context context, String appCaseNo) {
        this(context, appCaseNo, null, null, 1);
    }

    /**
     * @param context
     * @param appCaseNo
     * @param accidentType 选择的事故形态
     * @param discription  事故描述
     * @param carNumber    事故类型，单车，双车，三车
     */
    public AccidentLoadingView(Context context, String appCaseNo, String accidentType, String discription, Integer carNumber) {
        super(context);
        this.context = context;
        this.appCaseNo = appCaseNo;
        this.mApplication = (BaseApplication) context.getApplicationContext();
        if (carNumber != 1) {
            this.accidentType = accidentType;
            this.discription = discription;
            this.carNumber = carNumber;
        }
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        pictureRecognitionData();
    }

    private void initView() {
        switch (carNumber) {
            case 1:
                view = View.inflate(context, R.layout.view_accidentloadingsingle, null);
                initOneCar();
                break;
            case 2:
                view = View.inflate(context, R.layout.view_accidentloading, null);
                initTwoCar();
                break;
        }
        if(URLConstant.TEST.equals("true")){
            switch (carNumber) {
                case 1:
                    simulateJ();
                    break;
                case 2:
                    simulateJ();
                    simulateY();
                    break;
            }
        }
    }

    private void initCar() {
        bt_accidentloading = (Button) view.findViewById(R.id.bt_accidentloading);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("事故上传");
        //甲方选择车型
        stv_MotorVehicle_chooseCartypePartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_chooseCartypePartyJ);
        radioGroupMotorVehiclePartyJ = stv_MotorVehicle_chooseCartypePartyJ.getRadioGroup();
        //甲方姓名
        stv_MotorVehicle_NamePartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_NamePartyJ);
        stv_MotorVehicle_NamePartyJ.setTextModleParameter("姓名:");
        //甲方车牌
        stv_MotorVehicle_carNoPartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_carNoPartyJ);
        motorVehicle_provinceFirstViewPartyJ = stv_MotorVehicle_carNoPartyJ.getProvinceFirstView();
        //甲方车架
        stv_MotorVehicle_carFrameNoPartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_carFrameNoPartyJ);
        stv_MotorVehicle_carFrameNoPartyJ.setTextModleParameter("车架号:");
        //甲方联系电话
        stv_MotorVehicle_PhonePartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_PhonePartyJ);
        stv_MotorVehicle_PhonePartyJ.setTextModleParameter("联系电话:");
        //甲方驾驶证号
        stv_MotorVehicle_IDPartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_IDPartyJ);
        stv_MotorVehicle_IDPartyJ.setTextModleParameter("驾驶证号:");
        //甲方获取保单号
        stv_MotorVehicle_InsurancePartyJ = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_InsurancePartyJ);
        gainTextViewPartyJ = stv_MotorVehicle_InsurancePartyJ.getGainTextView();
    }

    private void initOneCar() {
        initCar();
        et_accidentSingle = (EditText) view.findViewById(R.id.et_accidentSingle);//事故地点
        tv_accidentSinglePlace = (TextView) view.findViewById(R.id.tv_accidentSinglePlace);//事故地点
        tv_accidentSingleTime = (TextView) view.findViewById(R.id.tv_accidentSingleTime);
        ll_accidentTime = (LinearLayout) view.findViewById(R.id.ll_accidentTime);
        ll_accidentTime.setOnClickListener(this);
        kckpUserType = mApplication.getKckpUserType();
        if (null != kckpUserType && "9".equals(kckpUserType)) {
            tv_accidentSinglePlace.setVisibility(View.GONE);
            et_accidentSingle.setVisibility(View.VISIBLE);
        } else {
            et_accidentSingle.setVisibility(View.GONE);
            tv_accidentSinglePlace.setVisibility(View.VISIBLE);
        }
    }

    private void initTwoCar() {
        initCar();
        //乙方选择车型
        stv_MotorVehicle_chooseCartypePartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_chooseCartypePartyY);
        radioGroupMotorVehiclePartyY = stv_MotorVehicle_chooseCartypePartyY.getRadioGroup();
        //姓名
        stv_MotorVehicle_NamePartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_NamePartyY);
        stv_MotorVehicle_NamePartyY.setTextModleParameter("姓名:");
        //车牌
        stv_MotorVehicle_carNoPartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_carNoPartyY);
        motorVehicle_provinceFirstViewPartyY = stv_MotorVehicle_carNoPartyY.getProvinceFirstView();
        //车架
        stv_MotorVehicle_carFrameNoPartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_carFrameNoPartyY);
        stv_MotorVehicle_carFrameNoPartyY.setTextModleParameter("车架号:");
        //联系电话
        stv_MotorVehicle_PhonePartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_PhonePartyY);
        stv_MotorVehicle_PhonePartyY.setTextModleParameter("联系电话:");
        //驾驶证号
        stv_MotorVehicle_IDPartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_IDPartyY);
        stv_MotorVehicle_IDPartyY.setTextModleParameter("驾驶证号:");
        //获取保单号
        stv_MotorVehicle_InsurancePartyY = (SetTextModule) view.findViewById(R.id.stv_MotorVehicle_InsurancePartyY);
        gainTextViewPartyY = stv_MotorVehicle_InsurancePartyY.getGainTextView();

    }

    private void regEvent(boolean b) {
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (null != bt_accidentloading) {
            bt_accidentloading.setOnClickListener(b ? this : null);
        }
        if (null != motorVehicle_provinceFirstViewPartyJ) {
            motorVehicle_provinceFirstViewPartyJ.setOnClickListener(b ? this : null);
        }
        if (null != gainTextViewPartyJ) {
            gainTextViewPartyJ.setOnClickListener(b ? this : null);
        }
        if (null != radioGroupMotorVehiclePartyJ) {
            radioGroupMotorVehiclePartyJ.setOnCheckedChangeListener(b ? this : null);
        }
        if (null != motorVehicle_provinceFirstViewPartyY) {
            motorVehicle_provinceFirstViewPartyY.setOnClickListener(b ? this : null);
        }
        if (null != radioGroupMotorVehiclePartyY) {
            radioGroupMotorVehiclePartyY.setOnCheckedChangeListener(b ? this : null);
        }
        if (null != gainTextViewPartyY) {
            gainTextViewPartyY.setOnClickListener(b ? this : null);
        }
        if (null != ll_accidentTime) {
            ll_accidentTime.setOnClickListener(b ? this : null);
        }
        if (b) {
            this.addILFMsgListener(this);
        } else {
            this.removeILFMsgListener(this);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        if (radioGroup == radioGroupMotorVehiclePartyJ) {
            if (i == stv_MotorVehicle_chooseCartypePartyJ.getRadioButtonOneId()) {
                jfCarType = "1";
            } else if (i == stv_MotorVehicle_chooseCartypePartyJ.getRadioButtonTwoId()) {
                jfCarType = "2";
            }
        } else if (radioGroup == radioGroupMotorVehiclePartyY) {
            if (i == stv_MotorVehicle_chooseCartypePartyY.getRadioButtonOneId()) {
                yfCarType = "1";
            } else if (i == stv_MotorVehicle_chooseCartypePartyY.getRadioButtonTwoId()) {
                yfCarType = "2";
            }
        }
    }

    private void getInfomationCar() {
        nameJ = stv_MotorVehicle_NamePartyJ.getTextModuleText();//甲方车主姓名
        firstCarNumJ = stv_MotorVehicle_carNoPartyJ.getProvinceText();//甲方车主车牌号首位
        secendCarNumJ = stv_MotorVehicle_carNoPartyJ.getProvinceNext(); //甲方车牌号次位
        carNumJ = stv_MotorVehicle_carNoPartyJ.getCarNo();//甲方车牌号
        checked1 = stv_MotorVehicle_carNoPartyJ.isChecked();//甲方是否临时牌照
        carVINJ = stv_MotorVehicle_carFrameNoPartyJ.getTextModuleText().toUpperCase(); //甲方车架号
        phoneJ = stv_MotorVehicle_PhonePartyJ.getTextModuleText(); //甲方手机号
        idCardJ = stv_MotorVehicle_IDPartyJ.getTextModuleText().toUpperCase(); //甲方驾驶证号
        insuranceJ = stv_MotorVehicle_InsurancePartyJ.getGainText();//甲方保单号
    }

    private void getInfomationOneCar() {
        getInfomationCar();
        accidentPlace = et_accidentSingle.getText().toString().trim();//单车的事故地点
    }

    private void getInfomationTwoCar() {
        getInfomationCar();
        nameY = stv_MotorVehicle_NamePartyY.getTextModuleText();//乙方车主姓名
        firstCarNumY = stv_MotorVehicle_carNoPartyY.getProvinceText(); //乙方车主车牌号首位
        secendCarNumY = stv_MotorVehicle_carNoPartyY.getProvinceNext(); //乙方车牌号次位
        carNumY = stv_MotorVehicle_carNoPartyY.getCarNo();  //乙方车牌号
        checked2 = stv_MotorVehicle_carNoPartyY.isChecked();//乙方是否临时牌照
        carVINY = stv_MotorVehicle_carFrameNoPartyY.getTextModuleText().toUpperCase();//乙方车架号
        phoneY = stv_MotorVehicle_PhonePartyY.getTextModuleText(); //乙方手机号
        idCardY = stv_MotorVehicle_IDPartyY.getTextModuleText().toUpperCase();//乙方驾驶证号
        insuranceY = stv_MotorVehicle_InsurancePartyY.getGainText(); //乙方保单号
    }

    @Override
    public void onClick(View v) {
        getInfomationCar();
        switch (carNumber) {
            case 1:getInfomationOneCar();break;
            case 2:getInfomationTwoCar();break;
        }
        if (v == motorVehicle_provinceFirstViewPartyJ) stv_MotorVehicle_carNoPartyJ.showPop();
        if (v == gainTextViewPartyJ) {
            stv_MotorVehicle_InsurancePartyJ.setGainText(""); //获取保单号
            getBxNumber(secendCarNumJ, carNumJ, carVINJ, stv_MotorVehicle_InsurancePartyJ.getGainEdittext());
        }
        if (v == motorVehicle_provinceFirstViewPartyY) stv_MotorVehicle_carNoPartyY.showPop();
        if (v == gainTextViewPartyY) {
            stv_MotorVehicle_InsurancePartyY.setGainText("");//获取保单号
            getBxNumber(secendCarNumY, carNumY, carVINY, stv_MotorVehicle_InsurancePartyY.getGainEdittext());
        }
        if(v.getId()==R.id.hn_btn_back) showHomeView();
        if(v.getId()==R.id.bt_accidentloading){
            switch (carNumber) {
                case 1:
                    if (!ifEdittextNullJ("")) return;
                    if (!verifyInformationJ("")) return;
                    if (!inProvinceJ()) return;
                    break;
                case 2:
                    if (!ifEdittextNullJ("甲方")) return;
                    if (!ifEdittextNullY()) return;
                    if (!verifyInformationJ("甲方")) return;
                    if (!verifyInformationY()) return;
                    if (!InformationJEqulsInformationY()) return;
                    if (!inProvinceJ()) return;
                    if (!inProvinceY()) return;
                    break;
            }
            if (carNumber == 1){
                try {
                    if(StringUtils.date(CommontUtils.getNowTime(),tv_accidentSingleTime.getText().toString())){
                        UiUtil.showToast(context, "事故时间不得晚于当前时间");
                        return;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                submitAccidentCase();
            }
            else testingCarNum();
        }
        if(v.getId()==R.id.ll_accidentTime) showTimePiker();
    }

    private void showTimePiker() {
        boolean[] type = new boolean[]{true, true, true, true, true, false};//显示类型 默认全部显示
        TimePickerView pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                tv_accidentSingleTime.setText(getTime(date));
            }
        }).setType(type)
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    private void simulateJ() {
        stv_MotorVehicle_NamePartyJ.setText("测试A");//甲方车主姓名
        stv_MotorVehicle_carNoPartyJ.getProvinceNextView().setText("A70827"); //甲方车牌号次位
        stv_MotorVehicle_carFrameNoPartyJ.setText("WBAKR0103E0J27794"); //甲方车架号
        stv_MotorVehicle_PhonePartyJ.setText("13554042403"); //甲方手机号
        stv_MotorVehicle_IDPartyJ.setText("410101199601010159"); //甲方驾驶证号
    }
    private void simulateY() {
        stv_MotorVehicle_NamePartyY.setText("测试B");
        stv_MotorVehicle_carNoPartyY.getProvinceNextView().setText("FB0095");
        stv_MotorVehicle_carFrameNoPartyY.setText("LB37102S9BH009039");
        stv_MotorVehicle_PhonePartyY.setText("15527051575");
        stv_MotorVehicle_IDPartyY.setText("420984199305165590");
    }

    private boolean inProvinceJ() {
        if (!firstCarNumJ.equals("豫") || checked1 ) {
            showDialog("请人工核实临牌或省外车辆的证件信息真伪");
            return false;
        }
        return true;
    }
    private boolean inProvinceY() {
        if (!firstCarNumY.equals("豫") || checked2) {
            showDialog("请人工核实临牌或省外车辆的证件信息真伪");
            return false;
        }
        return true;
    }

    private boolean InformationJEqulsInformationY() {
        //判断双方的车牌号是否相同
        if (carNumJ.equals(carNumY)) {
            UiUtil.showToast(context, "事故双方车牌号相同");
            return false;
        }
        //判断双方的车架号是否相同
        else if (carVINJ.equals(carVINY)) {
            UiUtil.showToast(context, "事故双方车架号相同");
            return false;
        }
        //判断双方的手机号是否相同
        else if (phoneJ.equals(phoneY)) {
            UiUtil.showToast(context, "事故双方联系电话相同");
            return false;
        }
        //判断双方的车架号是否相同
        else if (idCardJ.equals(idCardY)) {
            UiUtil.showToast(context, "事故双方驾驶证号相同");
            return false;
        }
        //判断双方的保单号是否相同
        else if (!(TextUtils.isEmpty(insuranceJ) && TextUtils.isEmpty(insuranceY))) {
            if (insuranceJ.equals(insuranceY)) {
                UiUtil.showToast(context, "事故双方保单号相同");
                return false;
            }
        }
        return true;
    }

    private boolean verifyInformationJ(String who) {
        //甲方验证
        if (!StringUtils.isCar(carNumJ)) {
            UiUtil.showToast(context, who+"车牌号错误");
            return false;
        } else if (!firstCarNumJ.equals("豫") || checked1) {
            if (!CommontUtils.isCarvin(carVINJ)) {
                UiUtil.showToast(context, who+"车架号错误");
                return false;
            }
        } else if (!StringUtils.isPhone(phoneJ)) {
            UiUtil.showToast(context, who+"联系电话错误");
            return false;
        } else {
            try {
                if (!CheckCard.IDCardValidate(idCardJ)) {
                    UiUtil.showToast(context, who+"驾驶证号错误");
                    return false;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    private boolean verifyInformationY() {
        //乙方验证
        if (!StringUtils.isCar(carNumY)) {
            UiUtil.showToast(context, "乙方车牌号错误");
            return false;
        } else if (!firstCarNumY.equals("豫") || checked2) {
            if (!CommontUtils.isCarvin(carVINY)) {
                UiUtil.showToast(context, "乙方车架号错误");
                return false;
            }
        } else if (!StringUtils.isPhone(phoneY)) {
            UiUtil.showToast(context, "乙方联系电话错误");
            return false;
        } else try {
            if (!CheckCard.IDCardValidate(idCardY)) {
                UiUtil.showToast(context, "乙方驾驶证号错误");
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    private void showDialog(String s) {
        UiUtils.showDialog(context, R.mipmap.warn, null, s, "人工核实", "认可提交",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (carNumber){
                            case 1:
                                submitAccidentCase();
                                break;
                            case 2:
                                testingCarNum();
                                break;
                        }
                    }
                });
    }

    /**
     * 验证车牌是否有交强险信息
     */
    private void testingCarNum() {
        try {
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean[] casecarlist;//车主信息数组
            EntityBean bean = new EntityBean();
            bean.set("appcaseno", appCaseNo);
            EntityBean yfBean = new EntityBean();// 乙方数据
            yfBean.set("carownname", nameY);// 车主姓名
            yfBean.set("carownphone", phoneY); //手机号
            yfBean.set("driverlicence", idCardY);//驾驶证号
            yfBean.set("casecarno", carNumY); // 案件车牌号
            yfBean.set("cartype", yfCarType); // 车型
            yfBean.set("frameno", carVINY);//车架号
            yfBean.set("cardno", idCardY); //身份证
            yfBean.set("trafficinsno", insuranceY); //保单号
            yfBean.set("drivernoimg", ""); //驾驶证照片
            yfBean.set("drivingnoimg", "");  //行驶证照片
            yfBean.set("party", "0");
            if (checked2) {
                yfBean.set("temporary", "1");//1为临时车牌
            } else {
                yfBean.set("temporary", "0");
            }
            EntityBean jfBean = new EntityBean();// 甲方数据
            jfBean.set("carownname", nameJ); // 车主姓名
            jfBean.set("carownphone", phoneJ);  // 电话
            jfBean.set("driverlicence", idCardJ);
            jfBean.set("casecarno", carNumJ);  // 案件车牌号
            jfBean.set("cartype", jfCarType); // 车型
            jfBean.set("frameno", carVINJ);  //车架号
            jfBean.set("cardno", idCardJ); //身份证
            jfBean.set("trafficinsno", insuranceJ); //保单号
            jfBean.set("drivernoimg", ""); //驾驶证照片
            jfBean.set("drivingnoimg", ""); //行驶证照片
            if (checked1) {
                jfBean.set("temporary", "1");//1为临时车牌
            } else {
                jfBean.set("temporary", "0");
            }
            jfBean.set("party", "1");//区分甲方乙方，甲方是1
            casecarlist = new EntityBean[]{yfBean, jfBean};
            bean.set("casecarlist", casecarlist);
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.JJAPPCHECKCBXX, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            String redes = bean.getString("redes");
                            if ("1".equals(restate)) {
                                submit();
                            } else {
                                showsingle(context, redes);
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    private void showsingle(final Context context, String redes) {
        UiUtils.showSingleDialog(context, R.mipmap.a_32, redes, "确认", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }

    //创造双方的bean跳转,这是责任认定书的bean[]
    private void submit() {
        EntityBean JfBean = new EntityBean();
        JfBean.set("carType", jfCarType);
        JfBean.set("carownname", nameJ);
        JfBean.set("casecarno", carNumJ);//甲方车牌号
        JfBean.set("casecarphone", phoneJ);
        JfBean.set("casecarId", idCardJ);
        JfBean.set("frameno", carVINJ);
        JfBean.set("trafficinsno", insuranceJ);//保单号
        JfBean.set("accidentplace", imageaddress);//事故地点
        EntityBean YfBean = new EntityBean();
        YfBean.set("carownname", nameY);
        YfBean.set("casecarno", carNumY);//乙方车牌号
        YfBean.set("casecarId", idCardY);//驾驶证号
        YfBean.set("casecarphone", phoneY);//联系电话
        YfBean.set("accidentType", accidentType);
        YfBean.set("discription", discription);
        YfBean.set("appCaseNo", appCaseNo);
        YfBean.set("carType", yfCarType);
        YfBean.set("trafficinsno", stv_MotorVehicle_InsurancePartyY.getGainText());//保单号
        YfBean.set("frameno", stv_MotorVehicle_carFrameNoPartyY.getTextModuleText().toUpperCase());
        EntityBean[] disputeBean = new EntityBean[]{JfBean, YfBean};
        DisputeView disputeview = new DisputeView(context, disputeBean);
        FrameworkManager.getInstance().showNewForm(context, disputeview);
    }

    /**
     * 判断输入框是否为空
     */
    private boolean ifEdittextNullJ(String who) {
        if (TextUtils.isEmpty(jfCarType)) {
            UiUtil.showToast(context, "请"+who+"选择车辆类型");
            return false;
        } else if (TextUtils.isEmpty(nameJ)) {
            UiUtil.showToast(context, who+"姓名为空");
            return false;
        } else if (TextUtils.isEmpty(secendCarNumJ)) {
            UiUtil.showToast(context,who+ "车牌号为空");
            return false;
        } else if (TextUtils.isEmpty(carVINJ)) {
            UiUtil.showToast(context, who+"车架号为空");
            return false;
        } else if (TextUtils.isEmpty(phoneJ)) {
            UiUtil.showToast(context, who+"联系电话为空");
            return false;
        } else if (TextUtils.isEmpty(idCardJ)) {
            UiUtil.showToast(context, who+"驾驶证号为空");
            return false;
        }
        return true;
    }

    /**
     * 判断输入框是否为空
     */
    private boolean ifEdittextNullY() {
        if (TextUtils.isEmpty(yfCarType)) {
            UiUtil.showToast(context, "请乙方选择车辆类型");
            return false;
        } else if (TextUtils.isEmpty(nameY)) {
            UiUtil.showToast(context, "乙方姓名为空");
            return false;
        } else if (TextUtils.isEmpty(secendCarNumY)) {
            UiUtil.showToast(context, "乙方车牌号为空");
            return false;
        } else if (TextUtils.isEmpty(carVINY)) {
            UiUtil.showToast(context, "乙方车架号为空");
            return false;
        } else if (TextUtils.isEmpty(phoneY)) {
            UiUtil.showToast(context, "乙方联系电话为空");
            return false;
        } else if (TextUtils.isEmpty(idCardY)) {
            UiUtil.showToast(context, "乙方驾驶证号为空");
            return false;
        }
        return true;
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            showHomeView();
        }
        return false;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        myHandler = null;
        regEvent(false);
        closeForm();
    }

    /**
     * 提示是否不继续处理，返回首页
     */
    private void showHomeView() {
        UiUtils.showDialog(context, null, null, "是否放弃输入，结束本次快处并返回首页", "返回", "确认",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        closeAllForm();
                        OnDestroy();
                        HomePageView homepageview = new HomePageView(context);
                        FrameworkManager.getInstance().showNewForm(context, homepageview);
                    }
                });
    }

    // 获取保单号
    private void getBxNumber(String car, String carno, String framno, final EditText view) {
        try {
            if (TextUtils.isEmpty(car)) {
                UiUtil.showToast(context, "请填写车牌号");
                return;
            }
            if (TextUtils.isEmpty(framno)) {
                UiUtil.showToast(context, "请填写车架号");
                return;
            }
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean bean = new EntityBean();
            bean.set("userflag", mApplication.getKckpName());
            bean.set("token", mApplication.getToken());
            bean.set("casecarno", carno);
            bean.set("appcaseno", appCaseNo);
            bean.set("policyno", "");
            bean.set("frameno", framno.toUpperCase());
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.JJAPPKCKPSEARCHCPSNUM, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            if ("1".equals(restate)) {
                                EntityBean data = bean.getBean("data");
                                String isValid = data.getString("isVaild");
                                if ("0".equals(isValid)) {
                                    String trafficinsno = data.getString("trafficinsno");
                                    view.setTextSize(12);
                                    view.setText(trafficinsno);
                                }
                            } else {
                                String error = bean.getString("redes");
                                UiUtil.showToast(context, error);
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (view == null) {
                return;
            }
            if (msg.what == 0001) {
                switch (carNumber){
                    case 1: setRecognitionOneCar();break;
                    case 2: setRecognitionJ();setRecognitionY();break;
                }
            }
        }
    };

    private void setRecognitionY() {
        EntityBean yfJszBean = mApplication.getYfJszBean();//乙方驾驶证
        if (null != yfJszBean) {
            String ocrname = yfJszBean.getString("ocrname");//姓名
            String ocrcardno = yfJszBean.getString("ocrcardno");//驾驶证号
            if (null != ocrname) {
                stv_MotorVehicle_NamePartyY.setText(ocrname);
            }
            if (null != ocrcardno) {
                stv_MotorVehicle_IDPartyY.setText(ocrcardno);
            }
        }
        EntityBean yfXszBean = mApplication.getYfXszBean();//乙方行驶证
        if (null != yfXszBean) {
            String ocrvin = yfXszBean.getString("ocrvin");//车架号
            String ocrcarno = yfXszBean.getString("ocrcarno");//车牌号
            if (null != ocrvin) {
                stv_MotorVehicle_carFrameNoPartyY.setText(ocrvin);
            }
            if (null != ocrcarno && StringUtils.isCar(ocrcarno)) {//车牌正确才
                stv_MotorVehicle_carNoPartyY.getProvinceFirstView().setText(ocrcarno.substring(0, 1));
                stv_MotorVehicle_carNoPartyY.getProvinceNextView().setText(ocrcarno.substring(1, 7));
            }
        }
    }

    private void setRecognitionOneCar(){
        //初始化信息，从拍照界面返的信息
        if (null!= kckpUserType &&"9".equals(kckpUserType)) et_accidentSingle.setText(imageaddress);
        else tv_accidentSinglePlace.setText(imageaddress);
        tv_accidentSingleTime.setText(casehaptime);
        setRecognitionJ();
    }
    private void setRecognitionJ() {
        EntityBean jfJszBean = mApplication.getJfJszBean();//甲方驾驶证
        if (null != jfJszBean) {
            String ocrname = jfJszBean.getString("ocrname");//姓名
            String ocrcardno = jfJszBean.getString("ocrcardno");//驾驶证号
            if (null != ocrname) {
                stv_MotorVehicle_NamePartyJ.setText(ocrname);
            }
            if (null != ocrcardno) {
                stv_MotorVehicle_IDPartyJ.setText(ocrcardno);
            }
        }
        EntityBean jfXszBean = mApplication.getJfXszBean();//甲方行驶证
        if (null != jfXszBean) {
            String ocrvin = jfXszBean.getString("ocrvin");//车架号
            String ocrcarno = jfXszBean.getString("ocrcarno");//车牌号
            if (null != ocrvin) {
                stv_MotorVehicle_carFrameNoPartyJ.setText(ocrvin);
            }
            if (null != ocrcarno && StringUtils.isCar(ocrcarno)) {//车牌正确才
                stv_MotorVehicle_carNoPartyJ.getProvinceFirstView().setText(ocrcarno.substring(0, 1));
                stv_MotorVehicle_carNoPartyJ.getProvinceNextView().setText(ocrcarno.substring(1, 7));
            }
        }
    }

    // 调取照片识别的接口
    private void pictureRecognitionData() {
        try {
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean bean = new EntityBean();
            String userName = mApplication.getLoginInfo().getBean("userinfo")
                    .getString("userflag");
            bean.set("appcaseno", appCaseNo);
            bean.set("userflag", userName);
            bean.set("token", mApplication.getToken());
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.JJAPPGETOCRDATA, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            String redes = bean.getString("redes");
                            imageaddress = bean.getString("imageaddress");
                            casehaptime = bean.getString("casehaptime");
                            EntityBean[] cardata = bean.getBeans("data");
                            if(null==cardata)return;
                            if ("1".equals(restate) && cardata != null) {
                                if(carNumber==1){
                                    for (EntityBean element : cardata) {
                                        String imagetype = element.getString("imagetype");
                                        if ("6".equals(imagetype)) {//甲驾驶证
                                            mApplication.setJfJszBean(element);
                                        } else if ("7".equals(imagetype)) {//甲方行驶证
                                            mApplication.setJfXszBean(element);
                                        }
                                    }
                                }else if(carNumber==2){
                                    for (EntityBean element : cardata) {
                                        String imagetype = element.getString("imagetype");
                                        if ("6".equals(imagetype)) {//甲驾驶证
                                            mApplication.setJfJszBean(element);
                                        } else if ("7".equals(imagetype)) {//甲方行驶证
                                            mApplication.setJfXszBean(element);
                                        } else if ("8".equals(imagetype)) {//乙方驾驶证
                                            mApplication.setYfJszBean(element);
                                        } else if ("9".equals(imagetype)) {//乙方行驶证
                                            mApplication.setYfXszBean(element);
                                        }
                                    }
                                }
                                if (myHandler != null) {
                                    myHandler.sendEmptyMessage(0001);
                                }
                            } else {
                                UiUtil.showToast(context, redes);
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    //事故上传
    private void submitAccidentCase() {
        processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        EntityBean bean = mApplication.getKckpRequestBean();
        bean.set("appcaseno", appCaseNo);
        bean.set("casecarno", carNumJ);
        bean.set("appphone", phoneJ);
        // 经度
        bean.set("caselon", mApplication.getLongitude());
        // 纬度
        bean.set("caselat", mApplication.getLatitude());
        bean.set("casehaplace", mApplication.getAddress());
        // 地址
        if (null != kckpUserType && "9".equals(kckpUserType))
            bean.set("accidentplace", et_accidentSingle.getText().toString().trim());
        else bean.set("accidentplace", tv_accidentSinglePlace.getText().toString().trim());
        // 提交时间
        bean.set("sibmitdate", CommontUtils.getNowTime());
        bean.set("areaid", mApplication.getAreaid()); // 区域
        bean.set("alarmtime", CommontUtils.getNowTime());
        if(carNumber==1) bean.set("casehaptime", tv_accidentSingleTime.getText().toString().trim());
        else bean.set("casehaptime",CommontUtils.getNowTime());
        bean.set("token", mApplication.getToken());
        bean.set("accidenttype", "");
        bean.set("accidentdes", "");
        bean.set("disposetype", "1");//增加一个自己还是交警定责的类型字段
        final EntityBean[] casecarlist = new EntityBean[1];//车主信息数组
        EntityBean jfBean = new EntityBean();
        if (checked1) jfBean.set("temporary", "1");//1为临时车牌
        else jfBean.set("temporary", "0");
        jfBean.set("carownname", nameJ);// 车主姓名
        jfBean.set("carownphone", phoneJ); //手机号
        jfBean.set("driverlicence", idCardJ); //
        jfBean.set("casecarno", carNumJ); // 案件车牌号
        jfBean.set("cartype", jfCarType); // 车型
        jfBean.set("frameno", carVINJ);//车架号
        jfBean.set("cardno", idCardJ); //身份证
        jfBean.set("trafficinsno", insuranceJ); //保单号
       /* bfBean.set("drivernoimg", ); //驾驶证照片
        bfBean.set("drivingnoimg", );  //行驶证照片*/
        jfBean.set("dutytype", "0");  //单车默认全责
        casecarlist[0] = jfBean;
        bean.set("casecarlist", casecarlist);
        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.HNJJAPPSUBMITCASEINFOR, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            UiUtil.showToast(context, "提交成功");
                            showNext();
                        } else {
                            showsingle(context, redes);
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        UiUtil.showToast(context, "连接超时，请检查网络后重试。");

                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });
    }

    private void showNext() {
        SingleCarOver singlecarover = new SingleCarOver(context, appCaseNo);
        FrameworkManager.getInstance().showNewForm(context, singlecarover);
    }
}
